package com.gaohe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaohe.common.BaseContext;
import com.gaohe.mapper.ClassesMapper;
import com.gaohe.mapper.TagsMapper;
import com.gaohe.pojo.Classes;
import com.gaohe.pojo.UserClass;
import com.gaohe.service.ClassesService;
import com.gaohe.service.UserClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes> implements ClassesService{
    @Autowired
    ClassesMapper classesMapper;
    @Override
    public List<Classes> getTeacherClasses(Long userId) {
        LambdaQueryWrapper<Classes>lw = new LambdaQueryWrapper<>();
        lw.eq(Classes::getTeacher_id,userId);
        List<Classes>list = classesMapper.selectList(lw);
        return list;
    }

    @Override
    public List<Classes> getClassByUserId(Long userid) {
        return classesMapper.getStudentAllPickedClass(userid);
    }

    @Autowired
    TagsMapper tagsMapper;
    @Override
    public List<String> getClassByCardTags(String tags) {
        // 1. 标签tags 解析
        String[] tagNames = tags.split(",");

        // 2.根据标签 找到相关课程
        LambdaQueryWrapper<Classes> qw = new LambdaQueryWrapper<>();
//         3. 查询课程的名字
        qw.select(Classes::getName);// 投影提高查询效率
        for (String tagName : tagNames) {
            qw.like(Classes::getTags,tagName).or();// 多条件or拼接
        }

        List<String> list = this.listObjs(qw);

        return list;
    }

    @Autowired
    UserClassService userClassService;
    @Override
    public List<Classes> getClassListByTag(String tag) {
//        1.获取用户id
        Long user_id = BaseContext.getCurrentId();

//        2.排除用户已选课程的id
        LambdaQueryWrapper<UserClass>lqw = new LambdaQueryWrapper<>();
        lqw.select(UserClass::getClassId);
        lqw.eq(UserClass::getUserId,user_id);
        List<Long>list = userClassService.listObjs(lqw);

        // 2.根据标签 找到相关课程
        LambdaQueryWrapper<Classes>lqw1 = new LambdaQueryWrapper<>();
        lqw1.like(Classes::getTags,tag);
        lqw1.notIn(!StringUtils.isEmpty(list)&&list.size()!=0, Classes::getId, list);
        List<Classes>list1 = this.list(lqw1);
        System.err.println(list1);
        return list1;
    }
}
